<?php

/*
 * 这不是一个开放软件，您不能用于商业应用
 * 不允许对程序代码以任何形式任何目的的再发布。
 * Copyright (c) 2015 YOUM.Co.Ltd. All rights reserved..
 * 网站地址：http://www.huishuojs.com
 * Author Zhyu
 */

/**
 * @author Zhyu
 * @date 2015-12-28 01:14:23
 * @copyright 杭州晖硕有码科技
 */
class System_money extends PC_Controller{
    protected $go_url = '/admin/system_money/index';
    //put your code here
    public function __construct() {
        parent::__construct();
        $this->load->model('system_money_log_model');
    }
    /**
     * 取现列表
     */
    public function index(){
        if($_GET){
            $this->data['phone'] =empty($_GET['phone'])?'':$_GET['phone']; 
        }
        $this->data['username'] =  isset($_POST['username'])? $_POST['username'] : '';
        $this->data['select_type']=  isset($_POST['select_type'])?$_POST['select_type']:'or';
        $this->data['start_time']= isset($_POST['start_time'])?$_POST['start_time']:'';
        $this->data['end_time']= isset($_POST['end_time'])?$_POST['end_time']:'';
        $message=$this->uri->segment(4)?$this->uri->segment(4):'1';
        $this->data['message']=$message;
         $this->data['error']    =  empty($_GET['message'])?'':$_GET['message'];
        $this->data['shenhe_all']=$this->Isallow('cash','shenhe_page');
        $this->load->view('/admin/system_money_log_index.php',$this->data);
    }
    /**
     * ajax 获取数据
     */
    public function ajax_system_money_log_list(){
       $data=$_GET;
        $start = $data['iDisplayStart']; //显示的起始索引
        $length = $data['iDisplayLength'];//显示的行数
        $sort_th = $data['mDataProp_'.$data['iSortCol_0'].''];//被排序的列
        $sort_type = $data['sSortDir_0'];//排序的方向 "desc" 或者 "asc".
        $search = $data ['sSearch'];//全局搜索字段
        $where=array();
        if(isset($data['user_id'])){
            $where['user_id']=$data['user_id'];
        }
   		 $where = '1=1 ';
		if ($this->admin ['admin_type'] == 'user') {
			$where.=' and user_id='.$this->admin['id'];
		}
		$userid_where = '';
		if ($this->admin ['admin_type'] == 'admin') {
			$userid_where = '';
			if (isset ( $data ['username'] ) && $data ['username']) {
				$sql = "SELECT id FROM user WHERE locate ('" . $data ['username'] . "' , user.username) > 0";
				$query = $this->db->query ( $sql );
				$user_ids = array ();
				foreach ( $query->result () as $row ) {
					$user_ids [] = $row->id;
				}
				$userid_where = " user_id in(" . implode ( ',', $user_ids ) . ")";
				unset ( $data ['username'] );
			}
		}
		$data_where = '';
		if (isset ( $data ['start_time'] ) && $data ['start_time']) {
			$data_where = 'create_time >=' . strtotime ( $data ['start_time'] );
			unset ( $data ['start_time'] );
		}
		if (isset ( $data ['end_time'] ) && $data ['end_time']) {
			$data_where .= ' and create_time <=' . strtotime ( $data ['end_time'] );
			unset ( $data ['end_time'] );
		}
		if ($data_where && $userid_where) {
			$where .= 'and (' . $userid_where . ' ' . $data ['select_type'] . ' ' . $data_where . ')';
		} else if ($data_where) {
			$where .= ' and ' . $data_where;
		} else if ($userid_where) {
			$where .= ' and ' . $userid_where;
		}
        
        
        
        $this->data['count'] = $this->system_money_log_model->getCount($where);
        $aaData=array();
        if($this->data['count']){
            $order_by = $sort_th." ".$sort_type;
            $system_money_log_list = $this->system_money_log_model->getSystemMoneyLogList($where,$length,$start,$order_by);
            $this->load->model('user_model');
            $users=array();
            $result=array();
            $is_shenhe=$this->Isallow('system_money', 'shenhe_page');
            foreach($system_money_log_list as $key=>$value){
                $shenhe_url='';
                switch ($value['status']){
                    case '0': 
                        $value['status_text']='已提交';
                        
                        if($is_shenhe){
                            //$shenhe_url = '<a href="/admin/system_money/shenhe_page/?id='.$value['id'].'"><span class="btn btn-xs btn-success cancel">审核</span></a>';
                        }
                        break;
                    case '1': 
                        $value['status_text']='已拒绝';
                        break;
                    case '2': 
                        $value['status_text']='已完成';
                        break;
                    case '3': 
                        $value['status_text']='已取消';
                        break;
                }
                if(isset($users[$value['user_id']])){
                    $value['true_name']=$users[$value['user_id']]['true_name'];
                }
                $value['create_time']=date('Y-m-d H:i:s',$value['create_time']);
                $value['last_time']=date('Y-m-d H:i:s',$value['last_time']);
                $user_info=$this->user_model->checkUser(array('id'=>$value['user_id']),'true_name,tixian');
                if($user_info){
                    $value['true_name']=$user_info['true_name'];
                    $users[$value['user_id']]['true_name']=$user_info['true_name'];
                }
                $value['operate']=$shenhe_url;
                $result[]=$value;
            }
               
            $aaData=$result;
            
        }
        $output['aaData'] = $aaData;
        $output['sEcho'] = $_GET['sEcho'];
        $output['iTotalDisplayRecords'] =  $this->data['count']; //总共有几条数据
        $output['iTotalRecords'] = $this->data['count']; //总共有几条数据
        echo json_encode($output); //最后把数据以json格式返回
    }
    /**
     *提现
     */
    public function add_page(){
        if(isset($_GET['user_id'])){
            $this->load->model('user_model');
            $user=$this->user_model->checkUser(array('id'=>$_GET['user_id'],'username,true_name,balance,tixian'));
            $this->data['true_name']=$user['true_name'];
            $this->data['username']=$user['username'];
            $this->data['balance']=$user['balance'];
            $this->data['tixian']=$user['tixian'];
            $this->load->view('/admin/system_money_add.php',  $this->data);
        }else{
            $this->location_href("/admin/user/user_list/6?parent_id=".$_GET['user_id']);
        }
    }
    /**
     * 添加申请转报单币
     */
    public function add(){
        $message=false;
        if(empty($_POST['username']) || empty($_POST['password'])){
            $message='参数错误';
            
        }else{
            $this->load->model('user_model');
            $user=$this->user_model->checkUser(array('username'=>$_POST['username']),'id,username,password2,true_name,money,freeze,balance,system_money,system_balance');
            if(empty($user)){
                $message='用户不存在';
            }else if($this->admin['admin_type']=='user' && ($user['balance']-$_POST['money'])<0){
					 $message='余额不足';
				}else{
                if($user['password2']==md5($_POST['password'])){
                    //$tax=$_POST['money']*0.08;
                    $feiyong=$_POST['money']*ZHUANBIN;
                    $data['user_id']=$user['id'];
                    $data['money']  =$_POST['money'];
                    $data['freeze']  =$_POST['money'];
                    $data['feiyong'] =$feiyong;
                    $data['shiji_money']=$_POST['money']-$feiyong;
                    $data['status'] =0;
                    $data['create_time']=time();
                    $data['last_time']  =time();
                    $data['operation']  =$this->admin['username'];
                    
                    $data['balance']    =$user['balance']-$_POST['money'];
                    
                    $result=$this->system_money_log_model->addSystemMoneyLog($data);
                    if($result){
                        $update_data['balance']=$data['balance'];
                        $update_data['freeze']=$user['freeze']+$_POST['money'];
                        $update_data['system_money']=$user['system_money']+$_POST['money']-$feiyong;
                        $update_data['system_balance']=$user['system_balance']+$_POST['money']-$feiyong;
                        $update=$this->user_model->editUser(array('id'=>$user['id']),$update_data);
                        $update=true;
                        if($update){
                            $logdata[]=array(
                                'user_id'=>$user['id'],
                                'username'=>$user['username'],
                                'true_name'=>$user['true_name'],
                                'money'=>'-'.$_POST['money'],
                                'type' =>'system_zhuan',
                                'remark'=>$user['true_name'].'您于'.date('Y-m-d H:i:s',time())."申请转报单币".$_POST['money']."元,扣除费用".$feiyong.'实际到账'.$data['shiji_money'],
                                'create_time'=>  time()
                            );
                            $this->add_money_log($logdata);
                            $this->location_href($this->go_url."/4");exit;
                        }else{
                            $this->system_money_log_model->deleteSystemMoneyLog(array('id'=>$result));
                            $message='添加失败';
                        }
                    }
                }else{
                    $message='交易密码错误';
                }
            }
        }
        $this->location_href($this->go_url."/5?message=".$message);
    }
    /**
     * 税收8%
     */
    public function tax(){
        
    }
    /**
     * 管理费
     */
    public function feiyong(){
        
    }
    /**
     * 验证二级密码
     */
    public function check_password(){
        $message=false;
        if(empty($_POST['username']) || empty($_POST['password'])){
            $message=false;
            
        }else{
            $this->load->model('user_model');
            $user=$this->user_model->checkUser(array('username'=>$_POST['username']),'username,password2');
            if(empty($user)){
                $message=false;
            }else{
                if($user['password2']==md5($_POST['password'])){
                    $message=true;
                }
            }
        }
        echo json_encode($message);
    }
    
}
